/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.markiewb.plugins.showlookupcontent;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.Lookup.Result;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.NbBundle.Messages;
import org.openide.util.Utilities;
import org.openide.windows.TopComponent;

/**
 * Top component which displays something.
 */
@TopComponent.Description(
    preferredID = "showlookupcontentTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "output", openAtStartup = false)
@ActionID(category = "Window", id = "de.markiewb.plugins.showlookupcontent.ShowLookupContentTopComponent")
@ActionReference(path = "Menu/Window/Other" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
    displayName = "#CTL_showlookupcontentAction",
preferredID = "showlookupcontentTopComponent")
@Messages({
    "CTL_showlookupcontentAction=Lookup content inspector",
    "CTL_showlookupcontentTopComponent=Lookup content inspector",
    "HINT_showlookupcontentTopComponent=This is the lookup content inspector. It shows the content of the current lookup."
})
public final class ShowLookupContentTopComponent extends TopComponent implements LookupListener {

    public ShowLookupContentTopComponent () {
        initComponents();
        setName(Bundle.CTL_showlookupcontentTopComponent());
        setToolTipText(Bundle.HINT_showlookupcontentTopComponent());

        lookupResult = Utilities.actionsGlobalContext().lookupResult(Object.class);

    }

    @Override
    public void resultChanged (LookupEvent ev) {
        Collection<? extends Object> allInstances = lookupResult.allInstances();
        txtTopComponent.setText(""+TopComponent.getRegistry().getActivated()+"\n");
        if (TopComponent.getRegistry().getActivated() != this) {
            txtLookupContent.setText("" + allInstances.size() + "\n");
            List<String> list = new ArrayList<String>();
            
            for (Object node : allInstances) {
                String text = node.getClass()+":              "+node;
                list.add(text);
            }
            Collections.sort(list);
            for (String text : list) {
                txtLookupContent.append(text+ "\n");
            }
            
            
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtTopComponent = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtLookupContent = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();

        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        txtTopComponent.setColumns(20);
        txtTopComponent.setRows(5);
        jScrollPane1.setViewportView(txtTopComponent);

        jSplitPane1.setLeftComponent(jScrollPane1);

        txtLookupContent.setColumns(20);
        txtLookupContent.setRows(5);
        jScrollPane3.setViewportView(txtLookupContent);

        jSplitPane1.setRightComponent(jScrollPane3);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(ShowLookupContentTopComponent.class, "ShowLookupContentTopComponent.jLabel1.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTextArea txtLookupContent;
    private javax.swing.JTextArea txtTopComponent;
    // End of variables declaration//GEN-END:variables
    Result<Object> lookupResult;

    @Override
    public void componentOpened () {

        lookupResult.addLookupListener(this);
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed () {
        lookupResult.removeLookupListener(this);
        // TODO add custom code on component closing
    }

    void writeProperties (java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties (java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
}
